#include <openssl/hmac.h>
#include <openssl/evp.h>
#include "core/auth/shahmac256_signer.h"

namespace aliyun {

string ShaHmac256Signer::GetSignatureMethod(void) {
  return "HMAC-SHA256";
}

string ShaHmac256Signer::GetSignatureVersion(void) {
  return "1.0";
}

string ShaHmac256Signer::SignString(const string& source, const string& accessSecret) {
  const EVP_MD* engine = EVP_sha256();
  HMAC_CTX ctx;
  HMAC_CTX_init(&ctx);
  HMAC_Init_ex(&ctx, (const unsigned char *)(accessSecret.c_str()), accessSecret.length(), engine, NULL);
  HMAC_Update(&ctx, (const unsigned char *)(source.c_str()), source.length());
  unsigned char sha256[EVP_MAX_MD_SIZE];
  unsigned int sha256_length;
  HMAC_Final(&ctx, sha256, &sha256_length);
  char base64[(EVP_MAX_MD_SIZE/3)*4];
  int base64_length = EVP_EncodeBlock((unsigned char *)(base64), sha256, sha256_length);
  return string(base64, 0, base64_length);
}

} // namespace aliyun {

// end of file
